/**
 * @param {number[][]} mat
 * @param {number} k
 * @return {number[]}
 */
export var kWeakestRows = function (mat, k) {
  for (let i = 0; i < mat.length; i++) {
    mat[i] = [count1(mat[i]), i]
  }
  return mat
    .sort((a, b) => a[0] - b[0])
    .slice(0, k)
    .map(item => item[1])
}

function count1(arr) {
  let left = 0,
    right = arr.length - 1
  while (left <= right) {
    let mid = Math.floor((left + right) / 2)
    if (arr[mid] === 1) {
      left = mid + 1
    } else {
      right = mid - 1
    }
  }
  return left
}
